package com.gaogzhen.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gaogzhen.domain.Config;
import com.gaogzhen.service.ConfigService;
import com.gaogzhen.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

/**
 * @author Administrator
 * @version 1.0
 * @description 参数配置
 * @date 2022-06-06 11:21
 */
@RestController
@RequestMapping("/configs")
@Api(tags = "参数配置")
public class ConfigController {

    @Autowired
    private ConfigService configService;

    @GetMapping
    @ApiOperation("分页查询配置参数")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "size", value = "每页大小"),
            @ApiImplicitParam(name = "type", value = "参数类型"),
            @ApiImplicitParam(name = "code", value = "参数编码"),
            @ApiImplicitParam(name = "name", value = "参数名称"),
    })
    @PreAuthorize("hasAuthority('config_query')")
    public R<Page<Config>> findByPage(@ApiIgnore Page<Config> page, String type, String code, String name) {
        return R.ok(configService.findByPage(page, type, code, name));
    }

    @PostMapping
    @ApiOperation("新增配置参数")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "config", value = "配置参数")
    })
    @PreAuthorize("hasAuthority('config_create')")
    public R add(@RequestBody @Validated Config config) {
        configService.add(config);
        return R.ok();
    }

    @PatchMapping
    @ApiOperation("修改配置参数")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "adminBank", value = "银行卡信息")
    })
    @PreAuthorize("hasAuthority('config_update')")
    public R update(@RequestBody @Validated Config config) {
        configService.updateConfig(config);
        return R.ok();
    }
}
